package com.zpz.framework.zpzoauth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultStatus;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthPageInfoExt;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthRequestUtil;
import com.zpz.framework.zpzoauth.dao.ZpzOauthDataAccessMapper;
import com.zpz.framework.zpzoauth.pojo.po.FrameDataAccess;
import com.zpz.framework.zpzoauth.pojo.qo.AddDataOauthQo;
import com.zpz.framework.zpzoauth.pojo.qo.DataOauthListQo;
import com.zpz.framework.zpzoauth.pojo.qo.DeleteFrameDataAccessQo;
import com.zpz.framework.zpzoauth.pojo.qo.ModifyFrameDataAccessQo;
import com.zpz.framework.zpzoauth.pojo.vo.DataOauthListVo;
import com.zpz.framework.zpzoauth.service.ZpzOauthDataAccessService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhaowen
 * @description 数据权限服务实现
 * @create 2019-07-22 10:50
 */
@Service
public class ZpzOauthDataAccessServiceImpl implements ZpzOauthDataAccessService {
    private Logger log = LoggerFactory.getLogger(ZpzOauthDataAccessServiceImpl.class);

    @Resource
    private ZpzOauthDataAccessMapper dataOauthAccessMapper;


    /**
     * @title 修改数据权限信息
     * @description
     * @author zhaowen
     * @param: request
     * @param: modifyFrameDataAccessQo
     * @updateTime 2019/7/22 11:19
     * @return: ZpzOauthResultMsg<java.lang.Boolean>
     * @throws
     */
    @Override
    public ZpzOauthResultMsg<Boolean> updateframedataAccess(HttpServletRequest request, ModifyFrameDataAccessQo modifyFrameDataAccessQo) {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        Map mapParam = new HashMap();
        mapParam.put("id",modifyFrameDataAccessQo.getId());
        FrameDataAccess frameDataAccess = dataOauthAccessMapper.selectDataOauth(mapParam);
        if(frameDataAccess==null)
        {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.UPDATE_DATAACCESS_FAILURE_31042);
        }
        frameDataAccess.setDataAccessCode(modifyFrameDataAccessQo.getAccessCode());
        frameDataAccess.setDataAccessVal(modifyFrameDataAccessQo.getAccessVal());
        frameDataAccess.setRemarks(modifyFrameDataAccessQo.getRemarks());
        frameDataAccess.setUpdateDate(new Date());
        frameDataAccess.setUpdateBy(ucodeLicense);
        Integer ret= dataOauthAccessMapper.updateFrameDataAccess(frameDataAccess);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.UPDATE_DATAACCESS_FAILURE_31040);
        }
    }

    /**
     * @title 删除数据权限信息
     * @description
     * @author zhaowen
     * @param: request
     * @param: deleteFrameDataAccessQo
     * @updateTime 2019/7/22 11:34
     * @return: ZpzOauthResultMsg<java.lang.Boolean>
     * @throws
     */
    @Override
    public ZpzOauthResultMsg<Boolean> delframedataAccess(HttpServletRequest request, DeleteFrameDataAccessQo deleteFrameDataAccessQo){
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        Map mapParam1 = new HashMap();
        mapParam1.put("id",deleteFrameDataAccessQo.getId());
        FrameDataAccess frameDataAccess = dataOauthAccessMapper.selectDataOauth(mapParam1);
        if(frameDataAccess==null)
        {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.UPDATE_DATAACCESS_FAILURE_31042);
        }
        Map mapParam = new HashMap();
        mapParam.put("id", deleteFrameDataAccessQo.getId());
        mapParam.put("updateBy", ucodeLicense);
        mapParam.put("updateDate", new Date());
        Integer ret=dataOauthAccessMapper.deleteFrameDataAccess(mapParam);
        if (ret>0){
            return ZpzOauthResultMsg.okForData(true);
        }else{
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DELETE_DATAACCESS_FAILURE_31041);
        }
    }

    /**
     * @name: selectDataOauthList
     * @description:查询数据权限列表
     * @returnType: ZpzOauthResultMsg<com.github.pagehelper.PageInfo<DataOauthListVo>>
     * @createBy: changyifan
     * @createDate: 2019-07-22 03:13:25
     * @updateBy:
     * @updateDate:
     * @param: [request, dataOauthListQo]
     * @throws Exception
     */
    @Override
    public ZpzOauthResultMsg<PageInfo<DataOauthListVo>> selectDataOauthList(HttpServletRequest request, DataOauthListQo dataOauthListQo) {
        PageHelper.startPage(dataOauthListQo.getPageNum(),dataOauthListQo.getPageSize(),true);
        List<FrameDataAccess> list = dataOauthAccessMapper.selectDataOauthList();
        if(list == null){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.DATA_IS_BLANK);
        }
        PageInfo<DataOauthListVo> pageInfo = new ZpzOauthPageInfoExt<FrameDataAccess,DataOauthListVo>().getPage(list,DataOauthListVo.class);
        return ZpzOauthResultMsg.okForData(pageInfo);
    }

    /**
     * @name: insertDataOauth
     * @description:新增数据权限
     * @returnType: ZpzOauthResultMsg<java.lang.Boolean>
     * @createBy: changyifan
     * @createDate: 2019-07-22 03:48:12
     * @updateBy:
     * @updateDate:
     * @param: [request, addDataOauthQo]
     * @throws Exception
     */
    @Override
    public ZpzOauthResultMsg<Boolean> insertDataOauth(HttpServletRequest request, AddDataOauthQo addDataOauthQo) {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        if(StringUtils.isBlank(addDataOauthQo.getDataAccessCode()) || StringUtils.isBlank(addDataOauthQo.getDataAccessVal())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.VERIFICATION_FAILURE_800019);
        }
        List<FrameDataAccess> list = dataOauthAccessMapper.selectDataOauthList();
        if(list != null && list.size() > 0) {
            for (FrameDataAccess l : list) {
                if (l.getDataAccessCode().equals(addDataOauthQo.getDataAccessCode())) {
                    return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ADD_ACCESS_EXISTCODE_31001);
                }
            }
            FrameDataAccess frameDataAccess = new FrameDataAccess();
            frameDataAccess.setDataAccessCode(addDataOauthQo.getDataAccessCode());
            frameDataAccess.setDataAccessVal(addDataOauthQo.getDataAccessVal());
            frameDataAccess.setCreateBy(ucodeLicense);
            frameDataAccess.setCreateDate(new Date());
            Integer result = dataOauthAccessMapper.insertDataOauth(frameDataAccess);
            if(result > 0){
                return ZpzOauthResultMsg.okForData(true);
            }else {
                return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ADD_ACCESS_ERROR_31000);
            }
        }
        return ZpzOauthResultMsg.okForData(null);
    }

}
